# Copyright (C) Kumo inc. and its affiliates.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

pypollux_add_module(
  legacy
  MODULE
  legacy/complex.cpp
  legacy/conversion.cpp
  legacy/pypollux.cpp
  legacy/serde.cpp
  legacy/signatures.cpp)

target_link_libraries(
  legacy
  PRIVATE
    pollux_type
    pollux_vector
    pollux_core
    pollux_exec
    pollux_parse_parser
    pollux_functions_prestosql
    pollux_functions_spark
    pollux_aggregates
    pollux_functions_spark_aggregates)

if(POLLUX_PYTHON_LEGACY_ONLY)
  # Only build the legacy module for CI
  return()
endif()

add_library(pollux_py_init init/PyInit.cpp)

target_link_libraries(
  pollux_py_init
  PRIVATE pollux_memory)
# This should not be necessary but the xxhash header is not part of any target
target_include_directories(pollux_py_init
                           PUBLIC ${PROJECT_SOURCE_DIR}/pollux/external/xxhash)

# pypollux.type library:
add_library(pollux_py_type_lib type/PyType.cpp)
target_link_libraries(
  pollux_py_type_lib pollux_type pybind11::module)

pypollux_add_module(type MODULE type/type.cpp)
target_link_libraries(
  type
  PRIVATE pollux_py_type_lib)

# pypollux.file library:
add_library(pollux_py_file_lib file/PyFile.cpp)
target_link_libraries(
  pollux_py_file_lib pollux_dwio_common pollux_py_init pybind11::module)

pypollux_add_module(file MODULE file/file.cpp)
target_link_libraries(
  file
  PRIVATE pollux_py_file_lib)

# pypollux.vector library:
add_library(pollux_py_vector_lib vector/PyVector.cpp)
target_link_libraries(
  pollux_py_vector_lib pollux_vector pybind11::module)

pypollux_add_module(vector MODULE vector/vector.cpp)
target_link_libraries(
  vector
  PRIVATE pollux_py_vector_lib)

# pypollux.plan_builder library:
add_library(pollux_py_plan_builder_lib plan_builder/PyPlanBuilder.cpp)
target_include_directories(pollux_py_plan_builder_lib
                           PUBLIC ${PROJECT_SOURCE_DIR}/pollux/external/xxhash)
target_link_libraries(
  pollux_py_plan_builder_lib
  pollux_py_type_lib
  pollux_py_vector_lib
  pollux_py_init
  pollux_vector
  pollux_core
  pollux_cursor
  pollux_hive_connector
  pollux_aggregates
  pollux_functions_prestosql
  pollux_parse_expression
  pollux_exec_test_lib
  pollux_tpch_connector
  pollux_dwio_dwrf_reader
  pollux_dwio_dwrf_common
  pollux_dwio_dwrf_writer
  Melon::melon
  pybind11::module)

pypollux_add_module(plan_builder MODULE plan_builder/plan_builder.cpp)
target_link_libraries(
  plan_builder
  PRIVATE pollux_py_plan_builder_lib)

# pypollux.runner library:
add_library(pollux_py_local_runner_lib runner/PyLocalRunner.cpp
                                      runner/PyConnectors.cpp)
target_include_directories(pollux_py_local_runner_lib
                           PUBLIC ${PROJECT_SOURCE_DIR}/pollux/external/xxhash)
target_link_libraries(
  pollux_py_local_runner_lib
  pollux_py_type_lib
  pollux_py_vector_lib
  pollux_py_init
  pollux_vector
  pollux_core
  pollux_cursor
  pollux_hive_connector
  pollux_exec_test_lib
  pollux_dwio_dwrf_reader
  pollux_dwio_dwrf_common
  pollux_dwio_dwrf_writer
  pybind11::module)

pypollux_add_module(runner MODULE runner/runner.cpp)
target_link_libraries(
  runner
  PRIVATE pollux_py_local_runner_lib)

# pyarrow has to be installed
find_package(pyarrow REQUIRED)

# 'arrow' target already exists
pypollux_add_module(arrow_module MODULE arrow/arrow.cpp)
set_target_properties(
  arrow_module
  PROPERTIES OUTPUT_NAME "arrow")

# This ensures the dynamic linker can find libarrow* dependencies at runtime
# from the installed pyarrow package without requiring LD_LIBRARY_PATH to be set
get_rpath_origin(_origin)
set_property(
  TARGET arrow_module
  PROPERTY INSTALL_RPATH "${_origin}/../pyarrow"
  APPEND)

target_link_libraries(
  arrow_module
  PRIVATE
    pollux_py_init
    pollux_py_vector_lib
    pollux_arrow_bridge
    pyarrow::dev
    pybind11::module)
